package com.activiti.action;

import java.util.ArrayList;
import java.util.List;

import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.identity.Group;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.task.Task;

import com.activiti.biz.ActIdUserBiz;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class Login extends ActionSupport {
	public ActIdUserBiz actIdUserBiz;
	public TaskService taskService;
	public IdentityService identityService;
	public HistoryService historyService;
	private RuntimeService runtimeService;
	public String username;
	public String password;
	public List<Task> tasks;
	public List<HistoricTaskInstance> myActiveTasks;
	public List<HistoricTaskInstance> myEndTasks;
	
	public String login(){
		if(actIdUserBiz.isValidUser(username, password)) {
//			List<Group> groups=identityService.createGroupQuery().groupMember(username).list();
//			Group group=groups.get(0);
			ActionContext.getContext().getSession().put("username", username);
//			tasks=taskService.createTaskQuery().taskCandidateGroup(group.getName()).orderByTaskCreateTime().asc().list();
//			myTasks=historyService.createHistoricTaskInstanceQuery().taskAssignee(username).list();
			return SUCCESS;
	}
		else return INPUT;
	}
	
	public String toMainPage(){
		String currentUsername=(String)ActionContext.getContext().getSession().get("username");
		List<Group> groups=identityService.createGroupQuery().groupMember(currentUsername).list();
		Group group=groups.get(0);
		tasks=taskService.createTaskQuery().taskCandidateGroup(group.getName()).orderByTaskCreateTime().asc().list();
		List<HistoricTaskInstance> myHisTasks=historyService.createHistoricTaskInstanceQuery().taskAssignee(currentUsername).taskName("报销申请").list();
		myEndTasks=new ArrayList<HistoricTaskInstance>();
		myActiveTasks=new ArrayList<HistoricTaskInstance>();
		for(HistoricTaskInstance temp:myHisTasks){
			if(runtimeService.createExecutionQuery().executionId(temp.getExecutionId()).singleResult()==null)
				myEndTasks.add(temp);
			else myActiveTasks.add(temp);
		}
		return SUCCESS;
	}
	
	public String logout(){
		ActionContext.getContext().getSession().clear();
		return INPUT;
	}
	
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public ActIdUserBiz getActIdUserBiz() {
		return actIdUserBiz;
	}
	public void setActIdUserBiz(ActIdUserBiz actIdUserBiz) {
		this.actIdUserBiz = actIdUserBiz;
	}
	public TaskService getTaskService() {
		return taskService;
	}
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}
	public List<Task> getTasks() {
		return tasks;
	}
	public void setTasks(List<Task> tasks) {
		this.tasks = tasks;
	}
	public IdentityService getIdentityService() {
		return identityService;
	}

	public void setIdentityService(IdentityService identityService) {
		this.identityService = identityService;
	}

	public HistoryService getHistoryService() {
		return historyService;
	}

	public void setHistoryService(HistoryService historyService) {
		this.historyService = historyService;
	}

	public RuntimeService getRuntimeService() {
		return runtimeService;
	}

	public void setRuntimeService(RuntimeService runtimeService) {
		this.runtimeService = runtimeService;
	}

	public List<HistoricTaskInstance> getMyActiveTasks() {
		return myActiveTasks;
	}

	public void setMyActiveTasks(List<HistoricTaskInstance> myActiveTasks) {
		this.myActiveTasks = myActiveTasks;
	}

	public List<HistoricTaskInstance> getMyEndTasks() {
		return myEndTasks;
	}

	public void setMyEndTasks(List<HistoricTaskInstance> myEndTasks) {
		this.myEndTasks = myEndTasks;
	}
	
	
	
}
